package com.pshcp.patient.business;

import com.external.common.WebServiceException;
import com.external.pay.PayService;
import com.external.pay.domain.PayPackageDTO;
import com.external.service.WsRequestService;
import com.external.ws.soap.entity.dto.UnpaidFeeViewDTO;
import com.external.ws.soap.entity.res.UnpaidFeeViewRes;
import com.pshcp.common.enums.RegPayStatusEnum;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.model.PatientUnpayFeeRecord;
import com.pshcp.patient.tool.PatientUnpaidTools;
import com.pshcp.service.PatientService;
import com.pshcp.service.PatientUnpayFeeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 诊间支付订单创建
 * @Date 2023-02-14 17:55
 */
@Service
public class PatientUnpaidFeeBusiness {

    @Autowired
    private PatientService patientService;

    @Autowired
    private WsRequestService wsRequestService;

    @Autowired
    private PatientUnpayFeeRecordService unpayFeeRecordService;

    @Transactional(rollbackFor = Exception.class)
    public void enterOrder(PatientUnpayFeeRecord feeRecord,Integer payMode,String thirdNo) throws WebServiceException {

        Patient patient = patientService.findById(feeRecord.getPatientId());

        UnpaidFeeViewDTO viewDTO = PatientUnpaidTools.buildWsViewDTO(feeRecord.getHisPatientId(), feeRecord.getUnpayFeeId());
        UnpaidFeeViewRes res = wsRequestService.unpaidFeeView(viewDTO);

        wsRequestService.payClinicConfirm(PatientUnpaidTools.buildWsPayConfirm(patient, feeRecord, res, payMode, thirdNo));

        feeRecord.setPayStatus(RegPayStatusEnum.PAY_OK.getCode());
        unpayFeeRecordService.update(feeRecord);
    }


}
